"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from typing import List

from do.plan_do import PlanDo
from server import db_session


class PlanDao:

    def get(self, plan_id) -> PlanDo:
        # 根据id返回数据
        plan_do = db_session.query(PlanDo).filter_by(id=plan_id).first()
        # print(plan_do.build)
        return plan_do

    def list(self) -> List[PlanDo]:
        # 返回所有
        return db_session.query(PlanDo).all()

    def save(self, plan_do: PlanDo):
        # 新增数据
        db_session.add(plan_do)
        db_session.commit()
        return plan_do.id

    def update(self, plan_do: PlanDo):
        # 修改数据
        db_session.query(PlanDo).filter_by(id=plan_do.id).update(plan_do.as_dict())
        db_session.commit()
        return plan_do.id

    def delete(self, plan_id):
        # 删除数据
        db_session.query(PlanDo).filter_by(id=plan_id).delete()
        db_session.commit()
        return plan_id

    def get_by_name(self, name) -> PlanDo:
        # 通过用例名称查询数据
        return db_session.query(PlanDo).filter_by(name=name).first()
